package com.teamall.web.xxgl.controller;

import com.teamall.core.common.JModelAndView;
import com.teamall.core.common.Page;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.util.MessageResourceUtils;
import com.teamall.framework.util.UploadFileUtil;
import com.teamall.framework.util.Utils;
import com.teamall.web.base.vo.ResResult;
import com.teamall.web.sys.db.po.SysFilePO;
import com.teamall.web.sys.db.po.SysInfoPO;
import com.teamall.web.sys.service.ISysFileService;
import com.teamall.web.sys.service.ISysInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author
 * @create 2017-07-08 23:29
 **/
@Controller
@RequestMapping("/xxgl")
public class XxglController {
    @Autowired
    private ISysInfoService sysInfoService;
    @Autowired
    private ISysFileService sysFileService;
    @RequestMapping("/main")
    public ModelAndView index(String infoType,Integer noticeId) { 
        ModelAndView modelAndView = new JModelAndView("/xxgl/xxgl_main");
        if (Utils.notEmpty(noticeId)){
            modelAndView.addObject("noticeId",noticeId);
        }else{
            modelAndView.addObject("noticeId",0);
        }
        if(Utils.notEmpty(infoType))
            modelAndView.addObject("infoType",infoType);
        else
            modelAndView.addObject("infoType",99);
        return modelAndView;
    }

    @RequestMapping("/fpzcList")
    public ModelAndView fpzcList(String infoType) {
        ModelAndView modelAndView = new JModelAndView("/xxgl/fpzc_list");
        modelAndView.addObject("infoType",infoType);
        return modelAndView;
    }
    @RequestMapping("/infoAdd")
    public ModelAndView infoAdd(String infoType,Integer id) throws BaseAppException{
        ModelAndView modelAndView = new JModelAndView("/xxgl/xxgl_info_add");
        if(Utils.notEmpty(id)){
            SysInfoPO sysInfoPO = sysInfoService.selectByPrimaryKey(id);
            modelAndView.addObject("sysInfoPO",sysInfoPO);
            modelAndView.addObject("infoType",infoType);
        }
        modelAndView.addObject("infoType",infoType);
        return modelAndView;
    }
    @RequestMapping("/getlistByType")
    @ResponseBody
    public Page<SysInfoPO> getlistByType(SysInfoPO record,Page<SysInfoPO> page) throws BaseAppException{
        ModelAndView modelAndView = new JModelAndView("/xxgl/fpzc_list");
        return  sysInfoService.selectByArgAndPage(record,page);
    }
    @RequestMapping("/add")
    @ResponseBody
    public int getlistByType(SysInfoPO record) throws BaseAppException{
        if(Utils.isEmpty(record.getInfoId())) {
            record.setPubDate(new Date());
            record.setReadNum("0");
            record.setIsTop(0);
            return  sysInfoService.add(record);
        }else{
            SysInfoPO sysInfoPO = sysInfoService.selectByPrimaryKey(record.getInfoId());
            sysInfoPO.setExpDate(record.getExpDate());
            sysInfoPO.setTitle(record.getTitle());
            sysInfoPO.setContent(record.getContent());
            return sysInfoService.update(sysInfoPO);
        }
    }
    @RequestMapping("/batchDel")
    @ResponseBody
    public int batchDel(String ids) throws BaseAppException{
        return  sysInfoService.batchDel(ids);
    }
    @RequestMapping("/setTop")
    @ResponseBody
    public int setTop(Integer id) throws BaseAppException{
        SysInfoPO sysInfoPO = sysInfoService.selectByPrimaryKey(id);
        sysInfoPO.setIsTop(1);
        return sysInfoService.update(sysInfoPO);
    }
    @RequestMapping("/cacelTop")
    @ResponseBody
    public int cacelTop(Integer id) throws BaseAppException{
        SysInfoPO sysInfoPO = sysInfoService.selectByPrimaryKey(id);
        sysInfoPO.setIsTop(0);
        return sysInfoService.update(sysInfoPO);
    }
    @RequestMapping("/detail")
    @ResponseBody
    public ModelAndView detail(Integer id) throws BaseAppException{
        ModelAndView modelAndView = new JModelAndView("/xxgl/xxgl_info_detail");
        SysInfoPO sysInfoPO = sysInfoService.selectByPrimaryKey(id);
        sysInfoPO.setReadNum(String.valueOf(Integer.valueOf(sysInfoPO.getReadNum()) +1));
        sysInfoService.update(sysInfoPO);
        if(Utils.notEmpty(sysInfoPO.getAtttchUrls())) {
            String[] urlArray = sysInfoPO.getAtttchUrls().split(",");
            for (String attachment : urlArray) {
                if(attachment.indexOf("=")!=-1){
                    String fileId = attachment.split("=")[1];
                    SysFilePO po = new SysFilePO();
                    po.setPhotoCode(fileId);
                    SysFilePO resultPO = sysFileService.selectByArg(po).get(0);
                    Map map = new HashMap<>();
                    map.put("file_attachment", attachment);
                    map.put("file_name", resultPO.getFileName());
                    map.put("file_extension", "");
                    sysInfoPO.getAttachList().add(map);
                }
            }
        }
        modelAndView.addObject("sysInfoPO",sysInfoPO);
        return modelAndView;
    }
    @RequestMapping(value = "/upload", consumes = "multipart/form-data")
    @ResponseBody
    public ResResult uploadfiles(HttpServletRequest request) throws BaseAppException{
        ResResult resResult = new ResResult();
        List<Map> resultList = UploadFileUtil.saveFiles1(request);
        resResult.setMsg("文件上传成功");
        resResult.setRspData(resultList);
        for (Map<String,String> map:resultList){
            SysFilePO po = new SysFilePO();
            po.setPhotoCode(map.get("imgId"));
            po.setFilePath(map.get("realPath"));
            po.setFileName(map.get("fileName"));
            sysFileService.add(po);
        }
        return resResult;
    }
    @RequestMapping(value = "/test")
    @ResponseBody
    public Map test() throws BaseAppException{
        Map map = new HashMap();
        return map;
    }
    /**
     * 保存文件
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getImg/*/*/*")
    public void doPost(HttpServletRequest request, HttpServletResponse response, String imgId)
            throws ServletException, IOException, BaseAppException {
        String uri = request.getRequestURI();
        String path = uri.substring(uri.indexOf("/",1),uri.length());
        path = path.substring(path.indexOf("/",1),path.length());
        path = path.substring(path.indexOf("/",1),path.length());
        String imgPath = MessageResourceUtils.getMessage("uplaodPath") + "/" + path;
        FileInputStream inputStream = new FileInputStream(imgPath);
        int i = inputStream.available();
        //byte数组用于存放图片字节数据
        byte[] buff = new byte[i];
        inputStream.read(buff);
        //记得关闭输入流
        inputStream.close();
        //设置发送到客户端的响应内容类型
        response.setHeader("Content-Type", "image/jped");
        OutputStream out = response.getOutputStream();
        out.write(buff);
        //关闭响应输出流
        out.close();
    }
}
